<?php

class App_Model_DbTable_Tag_Recommend extends Ext_Db_Table  
{  
	/** Table name */  
    protected $_name = 'tag_recommend';

	public function getAll($sortId)
	{
		if(!$sortId)
		{
			return "";
		}
		
		$db = $this->getAdapter();
		$select = $db->select();
		$select
			->from($this->_name, "*")
			->order('sequence ASC')
			->where('sortId = :sortId')
			;
		$params = array('sortId' => $sortId);
		$select->bind($params);
		
		$list = $db->fetchAll($select);
		return $list;
	}
	
	public function addNew($inputArray)
	{
		$sql_data_array = array();
		if(array_key_exists('tagId', $inputArray)) $sql_data_array['tagId'] = $inputArray['tagId'];
		if(array_key_exists('createTime', $inputArray)) $sql_data_array['createTime'] = $inputArray['createTime'];

		try {
			// 插入数据行并返回插入的行数
			$rows_affected = $this->insert($sql_data_array);
			// 最后插入的数据id   
			$last_insert_id = $this->getAdapter()->lastInsertId();
		}catch (Exception $e){
			trigger_error($e->getMessage(), E_USER_WARNING);
		}
		return $last_insert_id;
	}
	
	public function getHot($createTime, $num)
	{
		$db = $this->getAdapter();
		$select = $db->select();
		$select
			->from($this->_name, array("tagId", 'COUNT(tagId) AS total'))
			->order('total DESC')
			->group("tagId")
			->where('createTime > :createTime')
			->limit($num)
			;
		$params = array('createTime' => $createTime);
		$result = $db->fetchAll($select, $params);
		
		return $result;
	}
}